<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{ articles.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="articles.aut_photo"
        />
        <div class="text">
          <p class="name">{{ articles.aut_name }}</p>
          <p class="time">{{ articles.pubdate | fromNow }}</p>
        </div>
        <van-button round size="small" type="info" @click="followClick">{{
          articles.is_followed === true ? "+关注" : "-取消关注"
        }}</van-button>
      </div>
      <div class="content">
        <div v-html="articles.content"></div>
      </div>
      <br />
      <br />
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          icon="articles.attitude ==1? 'good-job':'good-job-o'"
          @click="likeClick"
        >
          {{ articles.attitude == 1 ? "取消点赞" : "点赞" }}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="disClick"
          >{{ articles.attitude == 0 ? "取消不喜欢" : "不喜欢" }}</van-button
        >
      </div>
    </div>
    <!-- /文章详情 -->
  </div>
</template>

<script>
//导入网络中间层
import {
  getArticlesinfo,
  fellowUser,
  unFollowUser,
  likingsUser,
  cancelLikings,
  // 对文章不喜欢
  dislikesArticle,
  // 取消对文章不喜欢
  canccelDislikes,
} from "./home.js";
export default {
  name: "articles",
  data() {
    return {
      articles: {},
    };
  },
  // 1.0发起请求获取文章详细信息
  async created() {
    let res = await getArticlesinfo({ id: this.$route.query.id });
    // console.log(res);
    // 2.0绑定数据
    this.articles = res.data.data;
  },
  methods: {
    // 关注取消关注按钮功能
    async followClick() {
      // 判断状态
      if (this.articles.is_followed === true) {
        // 如果目前是已经关注 点击后取关
        let res = await unFollowUser({ target: this.articles.aut_id });
        //  弹出提示
        this.$toast.success("取关成功");
        this.articles.is_followed = false;
      } else {
        let res = await fellowUser({ target: this.articles.aut_id });
        this.$toast.success("关注成功");
        this.articles.is_followed = true;
      }
    },
    // 文章点赞 取消点赞功能
    async likeClick() {
      // 1.0如果现在是点赞状态 1 则取消点赞
      if (this.articles.attitude == 1) {
        let res = await cancelLikings({ target: this.articles.art_id });
        if (res.status === 204) {
          this.$toast.success("取消点赞成功");
          this.articles.attitude = -1;
        } else {
          this.$toast.success("取消失败");
        }
      } else {
        //  反之-1对用户经行点赞
        let res = await likingsUser({ target: this.articles.art_id });
        if (res.status === 201) {
          this.$toast.success("点赞成功");
          this.articles.attitude = 1;
          //  console.log(res)
        } else {
          this.$toast.success("点赞失败");
        }
      }
    },
    // 文章喜欢 不喜欢功能
    async disClick() {
      // 如果是1代表不喜欢  0代表显示取消不喜欢  -1  无态度
      if (this.articles.attitude == 0) {
        // 对文章不喜欢
        let res = await canccelDislikes({ target: this.articles.art_id });
        if (res.status === 204) {
          this.$toast.success("取消成功");
          this.articles.attitude = -1;
        } else {
          this.$toast.success("取消失败");
        }
      } else {
        // 取消不喜欢后改为 不喜欢 这个按钮
        let res = await dislikesArticle({ target: this.articles.art_id });
        if (res.status === 201) {
          this.$toast.success("操作成功");
          this.articles.attitude = 0;
        } else {
          this.$toast.success("操作失败");
        }
      }
    },
  },
};
</script>

<style lang="less">
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
  .van-button--danger {
    width: 100px;
  }
  .van-nav-bar__content {
    background-color: #fff;
    .van-nav-bar__left {
      .van-icon-arrow-left {
        color: #007acc;
      }
    }
    .van-nav-bar__title {
      color: black;
    }
  }
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>